/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.navigationbar;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.model.listener.NoDoubleClickListener;
import com.ecarx.systemui.plugin.utils.AppUtils;
import com.ecarx.systemui.plugin.utils.LogUtils;

public class NavigationBarClimateFanView extends FrameLayout {

    private static final String TAG = "NavigationBarClimateFanViewTag";

    private ImageView ivClimateFanSpeed;
    private ImageView ivClimateSpeedMode;
    private TextView tvClimate;

    public NavigationBarClimateFanView(@NonNull Context context) {
        this(context, null);
    }

    public NavigationBarClimateFanView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBarClimateFanView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.navigation_climate_center, this, true);
        ivClimateFanSpeed = findViewById(R.id.iv_climate_fan_speed);
        ivClimateSpeedMode = findViewById(R.id.iv_climate_speed_mode);
        tvClimate = findViewById(R.id.tv_climate);
        setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                LogUtils.i("Climate fan view Clicked ");
                DialogManager.getDialogManager().disMissAllDialog();
                AppUtils.startClimate(getContext(), true);
            }
        });
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (ivClimateFanSpeed != null) {
            ivClimateFanSpeed.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.hand_speed_level_0));
        }
        if (ivClimateSpeedMode != null) {
            ivClimateSpeedMode.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.wind_off));
        }
        if (tvClimate != null) {
            tvClimate.setTextColor(ContextCompat.getColor(getContext(), R.color.color_temp_text));
        }
    }

    public void updateFanLevel(boolean isAuto,int handFanValueLevel) {
        int resId = AppConstants.getCenterFanIconByFanValue(isAuto,handFanValueLevel);
        if (resId == 0) {
            return;
        }
        ivClimateFanSpeed.setImageDrawable(AppCompatResources.getDrawable(getContext(), resId));
    }
}
